package vista;

import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.Image;

import javax.swing.DefaultComboBoxModel;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;

import controlador.Controlador;
import controlador.DespacharCtrl;

import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JLabel;

import java.awt.Font;

import javax.swing.JTextField;
import javax.swing.JComboBox;
import javax.swing.border.BevelBorder;

import java.awt.event.FocusAdapter;
import java.awt.event.FocusEvent;

public class Despachar extends JDialog {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private final JPanel contentPanel = new JPanel();

	private Controlador controlador;
	private JTextField txtObs;
	private JTextField txtCantidad;
	private JTextField txtVentaUnidad;
	private JTextField txtGanancia;
	private JTextField txtCostoUnidad;
	private JTextField txtTotalVenta;
	@SuppressWarnings("rawtypes")
	private JComboBox cbxItemId;
	@SuppressWarnings("rawtypes")
	private JComboBox cbxCentroCosto;
	@SuppressWarnings("rawtypes")
	private JComboBox cbxItem;
	private JLabel lblFoto;
	private JTextField txtEnStock;
	/**
	 * Create the dialog.
	 */
	@SuppressWarnings("rawtypes")
	public Despachar() {
		setResizable(false);
		setTitle("Despachar");
		setIconImage(Toolkit.getDefaultToolkit().getImage(Despachar.class.getResource("/modelo/imagenes/cute_pictures_2921cd4.jpg")));
		setBounds(100, 100, 450, 300);
		setLocationRelativeTo(null);
		getContentPane().setLayout(new BorderLayout());
		contentPanel.setBorder(new EmptyBorder(5, 5, 5, 5));
		getContentPane().add(contentPanel, BorderLayout.CENTER);
		contentPanel.setLayout(null);
		
		JLabel lblItem = new JLabel("Item");
		lblItem.setBounds(10, 47, 80, 14);
		contentPanel.add(lblItem);
		
		JLabel lblNewLabel = new JLabel("Cantidad");
		lblNewLabel.setBounds(10, 97, 80, 14);
		contentPanel.add(lblNewLabel);
		
		JLabel lblVenta = new JLabel("Venta Unidad");
		lblVenta.setBounds(10, 122, 80, 14);
		contentPanel.add(lblVenta);
		
		JLabel lblGanancia = new JLabel("Ganancia");
		lblGanancia.setBounds(10, 204, 60, 14);
		contentPanel.add(lblGanancia);
		
		{
			JLabel lblDespacharStock = new JLabel("Despachar stock");
			lblDespacharStock.setFont(new Font("Tahoma", Font.PLAIN, 20));
			lblDespacharStock.setBounds(10, 11, 204, 25);
			contentPanel.add(lblDespacharStock);
		}
		
		JLabel lblTotalVenta = new JLabel("Total Venta");
		lblTotalVenta.setBounds(287, 204, 67, 14);
		contentPanel.add(lblTotalVenta);
		
		JLabel label = new JLabel("Centro de Costo");
		label.setBounds(223, 47, 80, 14);
//		contentPanel.add(label);
		
		JLabel lblCostoUnidad = new JLabel("Costo Unidad");
		lblCostoUnidad.setBounds(143, 204, 80, 14);
		contentPanel.add(lblCostoUnidad);
		
		JLabel lblObservaciones = new JLabel("Observaciones");
		lblObservaciones.setBounds(10, 147, 106, 14);
		contentPanel.add(lblObservaciones);
		
		cbxCentroCosto = new JComboBox();
		cbxCentroCosto.setBounds(313, 44, 89, 20);
//		contentPanel.add(cbxCentroCosto);
		
		cbxItem = new JComboBox();
		cbxItem.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				if (!(cbxItem.getSelectedIndex()==0)) {
				cbxItemId.setSelectedIndex(cbxItem.getSelectedIndex());
				controlador.getDespacharCtrl().buscarImagen(cbxItemId.getSelectedItem().toString(), lblFoto.getHeight(), lblFoto.getWidth());
				txtCostoUnidad.setText(controlador.getDespacharCtrl().buscarCostoUnidad(cbxItemId.getSelectedItem().toString()));
				txtEnStock.setText(controlador.getDespacharCtrl().buscarEnStock(cbxItemId.getSelectedItem().toString()));
				}else{
					borrarCampos();
				}
				
			}
		});
		cbxItem.setBounds(10, 66, 392, 20);
		contentPanel.add(cbxItem);
		
		lblFoto = new JLabel("");
		lblFoto.setBorder(new BevelBorder(BevelBorder.LOWERED, null, null, null, null));
		lblFoto.setBounds(316, 97, 86, 98);
		contentPanel.add(lblFoto);
		
		txtObs = new JTextField();
		txtObs.setBounds(10, 161, 293, 20);
		contentPanel.add(txtObs);
		txtObs.setColumns(10);
		
		txtCantidad = new JTextField();
		txtCantidad.addFocusListener(new FocusAdapter() {
			@Override
			public void focusLost(FocusEvent e) {
				calcularUnidad();
			}
		});
		txtCantidad.setBounds(95, 94, 86, 20);
		contentPanel.add(txtCantidad);
		txtCantidad.setColumns(10);
		
		txtVentaUnidad = new JTextField();
		txtVentaUnidad.addFocusListener(new FocusAdapter() {
			@Override
			public void focusLost(FocusEvent arg0) {
				calcularUnidad();
			}
		});
		txtVentaUnidad.setBounds(95, 122, 86, 20);
		contentPanel.add(txtVentaUnidad);
		txtVentaUnidad.setColumns(10);
		
		txtGanancia = new JTextField();
		txtGanancia.setEditable(false);
		txtGanancia.setBounds(73, 201, 60, 20);
		contentPanel.add(txtGanancia);
		txtGanancia.setColumns(10);
		
		txtCostoUnidad = new JTextField();
		txtCostoUnidad.setEditable(false);
		txtCostoUnidad.setBounds(217, 201, 60, 20);
		contentPanel.add(txtCostoUnidad);
		txtCostoUnidad.setColumns(10);
		
		txtTotalVenta = new JTextField();
		txtTotalVenta.setEditable(false);
		txtTotalVenta.setBounds(357, 201, 67, 20);
		contentPanel.add(txtTotalVenta);
		txtTotalVenta.setColumns(10);
		
		JLabel lblEnStock = new JLabel("En Stock");
		lblEnStock.setBounds(223, 97, 80, 14);
		contentPanel.add(lblEnStock);
		
		txtEnStock = new JTextField();
		txtEnStock.setEditable(false);
		txtEnStock.setBounds(217, 119, 86, 20);
		contentPanel.add(txtEnStock);
		txtEnStock.setColumns(10);
		
		cbxItemId = new JComboBox();
		cbxItemId.setBounds(95, 44, 86, 20);
//		contentPanel.add(cbxItemId);
		{
			JPanel buttonPane = new JPanel();
			buttonPane.setLayout(new FlowLayout(FlowLayout.RIGHT));
			getContentPane().add(buttonPane, BorderLayout.SOUTH);
			{
				JButton okButton = new JButton("OK");
				okButton.addActionListener(new ActionListener() {
					public void actionPerformed(ActionEvent e) {
						DespacharCtrl despacharCtrl = controlador.getDespacharCtrl();
						despacharCtrl.setTxtItemId(cbxItemId.getSelectedItem().toString());
						despacharCtrl.setTxtCantidad(txtCantidad.getText());
						despacharCtrl.setTxtCostoUnidad(txtCostoUnidad.getText());
						despacharCtrl.setTxtGanancia(txtGanancia.getText());
						despacharCtrl.setTxtVentaUnidad(txtVentaUnidad.getText());
						despacharCtrl.setTxtTotalVenta(txtTotalVenta.getText());
						despacharCtrl.setTxtObs(txtObs.getText());
						despacharCtrl.setTxtCentroCosto(cbxCentroCosto.getSelectedItem().toString());
						
						int cantidad=0, stock=0;
						
						try {
							cantidad = Integer.parseInt(txtCantidad.getText());
							stock = Integer.parseInt(txtEnStock.getText());
						} catch (Exception e2) {
							// TODO: handle exception
						}

						if(cantidad > 0 ){
							if (stock >= cantidad) {
								if (despacharCtrl.ingresarVenta()) {
									cerrarVentana();
								}
							} else {
								JOptionPane.showMessageDialog(null, "No hay Stock disponible para esta venta", "Advertencia", JOptionPane.WARNING_MESSAGE);
							}							
						}else {
							JOptionPane.showMessageDialog(null, "Cantidad incorrecta", "Advertencia", JOptionPane.WARNING_MESSAGE);
						}
					}
				});
				okButton.setActionCommand("OK");
				buttonPane.add(okButton);
				getRootPane().setDefaultButton(okButton);
			}
			{
				JButton cancelButton = new JButton("Cancel");
				cancelButton.addActionListener(new ActionListener() {
					public void actionPerformed(ActionEvent e) {
						cerrarVentana();						
					}
				});
				cancelButton.setActionCommand("Cancel");
				buttonPane.add(cancelButton);
			}
		}
	}
	public void borrarCampos(){
		lblFoto.setIcon(null);
		txtCantidad.setText("");
		txtTotalVenta.setText("");
		txtCostoUnidad.setText("");
		txtGanancia.setText("");
		txtVentaUnidad.setText("");
		txtObs.setText("");	
		txtEnStock.setText("");
	}
	
	protected void cerrarVentana() {
		controlador.ocultarVentanaDespachar();
		borrarCampos();		
	}

	public void setControlador(Controlador miControlador) {
		this.controlador = miControlador;		
	}

	@SuppressWarnings({ "unchecked", "rawtypes" })
	public void setModeloComboboxItem(DefaultComboBoxModel buscarItems) {
		cbxItem.setModel(buscarItems);
		
	}

	@SuppressWarnings({ "unchecked", "rawtypes" })
	public void setModeloComboboxCentroCosto(DefaultComboBoxModel buscarCentroCosto) {
		cbxCentroCosto.setModel(buscarCentroCosto);
		
	}

	@SuppressWarnings({ "unchecked", "rawtypes" })
	public void setModeloCbxItemId(DefaultComboBoxModel buscarItemsId) {
		cbxItemId.setModel(buscarItemsId);
		
	}

	public void cambiarImagen(Image icon) {
		lblFoto.setIcon(new ImageIcon(icon));
		
	}
	private void calcularUnidad() {
		
		int a=0, b=0, c=0, d=0, e=0;
		
		try {
			a = Integer.parseInt(txtVentaUnidad.getText());
			b = Integer.parseInt(txtCantidad.getText());
			d = Integer.parseInt(txtCostoUnidad.getText());
			if(b!=0)			
				c = a * b;
				e = c - (b * d);
		} catch (Exception ex) {

		}		
		txtTotalVenta.setText(""+c);
		txtGanancia.setText(""+e);
	}
}
